//app.js
App({
  
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    //this.getOpenid();
  },
  getOpenid: function () {
    console.log("我是全局方法getOpenid");
    wx.login({
      //获取code
      success: function (res) {
        var code = res.code //返回code
        console.log('我是全局方法里的code=' + code)
        wx.request({
          url: getApp().globalData.API_URI + '/demo2.php',
          data: { //appid 与 secret为对应小程序项目后台获取
            'code': code,
            'appid': getApp().globalData.appid,
            'secret': getApp().globalData.secret,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            var openid = res.data
            console.log('我是全局方法里的openid=' + openid);
            getApp().globalData.openid = openid;
            getApp().getWXUserInfo();
          }
        })
      }
    })
  },
  getWXUserInfo: function () {
    console.log("我是getWXUserInfo方法")
    wx.request({
      url: getApp().globalData.API_URI + '/index.php/face_getUserInfo',
      data: { //appid 与 secret为对应小程序项目后台获取
        'openid': getApp().globalData.openid,
      },
      header: {//请求头  
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data)
        
        
        if (!res.data.error) {
          console.log("这里是全局变量的dlts==0")
          getApp().globalData.nickName = res.data.nickname;
          getApp().globalData.avatarUrl = res.data.avatar_url;
          getApp().globalData.sex = res.data.sex;
          getApp().globalData.dlts = "0";
        } else {
          // wx.navigateTo({
          //   url: '../authorization/index',
          // })
          console.log("这里是全局变量的dlts==1")
          getApp().globalData.dlts = "1";
        }
      }
    })
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  globalData: {
    userInfo: null,
    nickName: '',
    openid: '',
    avatarUrl: '',
    img_url:"",
    people_type1: "",
    share_title: "",
    
    pics:[],
    //魔图
    appid: 'wxc774742141a52509',
    secret: 'd1acae139cb22fd05aa55fb7a14cba27',
    //正式环境
    API_URI:'https://api.cndzys.com',
    //测试环境
    //API_URI: 'https://apitest.cndzys.com',
  }

})  